package com.example.zg.learn.customView;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 描述：
 * zhougan
 * 2019/2/12
 **/
public class MView extends View {
    private static final String TAG = ">>>>>MView";
    private String text="我是一个大xcvxcvxcvxcv消息称v多数的数数数数数数数数数地方";
    private Paint mPaint;
    private float startX=0;

    public MView(Context context) {
        super(context);
    }

    public MView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void initPaint(){
        if(mPaint==null){
            mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setTextSize(30);
            mPaint.setColor(Color.RED);
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(1);
            mPaint.setStyle(Paint.Style.FILL);
        }
    }


    public void setStartX(float startX) {
        this.startX = startX;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        initPaint();
        if(text!=null){
            canvas.drawText(text,startX,getMeasuredHeight()/2,mPaint);
        }
        Log.i(TAG, "onDraw: startX="+startX);


        super.onDraw(canvas);
    }

    public void startAnim(){
        ValueAnimator animator = ValueAnimator.ofFloat(startX, -mPaint.measureText(text));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                startX= (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(10000);
        animator.start();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        startX=getMeasuredWidth();
    }
}
